import request from '@/utils/request'

// 获取所有商品列表
export function getGoodsList({ page, tab, limit, title, category_id }) {
  if (title === null && category_id === null) {
    return request({
      url: `/goods/${page}?tab=${tab}&limit=${limit}`,
      method: 'get'
    })
  } else if (category_id === null) {
    return request({
      url: `/goods/${page}?tab=${tab}&limit=${limit}&title=${title}`,
      method: 'get'
    })
  } else if (title === null) {
    return request({
      url: `/goods/${page}?tab=${tab}&limit=${limit}&category_id=${category_id}`,
      method: 'get'
    })
  } else {
    return request({
      url: `/goods/${page}?tab=${tab}&limit=${limit}&title=${title}&category_id=${category_id}`,
      method: 'get'
    })
  }
}

// 新增商品
export function addGoods(data) {
  return request({
    url: '/goods',
    method: 'post',
    data
  })
}

// 修改商品
export function editGoods(id, data) {
  return request({
    url: `/goods/${id}`,
    method: 'post',
    data
  })
}

// 批量删除商品 data: {"ids":[61]} 商品id
export function deleteGoods(ids) {
  return request({
    url: '/goods/delete_all',
    method: 'post',
    data: {
      ids
    }
  })
}

// 恢复已删除的商品 data: {"ids":[61]} 商品id
export function restoreGoods(ids) {
  return request({
    url: '/goods/restore',
    method: 'post',
    data: {
      ids
    }
  })
}

// 彻底删除商品 data: {"ids":[61]} 商品id
export function destroyGoods(ids) {
  return request({
    url: '/goods/destroy',
    method: 'post',
    data: {
      ids
    }
  })
}

// 商品上架下架 data: {"ids":[61],"status":0}
export function changeStatus({ ids, status }) {
  return request({
    url: '/goods/changestatus',
    method: 'post',
    data: {
      ids,
      status
    }
  })
}

// 商品审核状态 id: 商品id  ischeck: 审核 1同意 2拒绝
export function changeCheck({ id, ischeck }) {
  return request({
    url: `/goods/${id}/check`,
    method: 'post',
    data: {
      ischeck
    }
  })
}

// 获取商品规格
export function getGoodsSkus(id) {
  return request({
    url: `/goods/read/${id}`,
    method: 'get'
  })
}

// 更新商品规格
export function updateGoodsSkus(id, data) {
  return request({
    url: `/goods/read/${id}`,
    method: 'post',
    data
  })
}

// 添加商品规格选相关
export function addGoodsSkus(data) {
  return request({
    url: '/goods_skus_card',
    method: 'post',
    data
  })
}

